#include "Bonus.h"
#include "Physics.h"

CBonus::CBonus()
{
	//non-ODE init routnes
	m_fSpeed = 2.0f;
	m_iSignZ = 0;	//no sign
	m_iValue = 100;
	m_fSize = 0.9f;

	//ODE init routines
	m_body = dBodyCreate( m_pPhysics->GetWorld() );
		

	m_geom = dCreateBox(m_pPhysics->GetSpace(), m_fSize, m_fSize, m_fSize);
		
	dGeomSetBody(m_geom, m_body);

	dMassSetBox(&m_mass, 5, m_fSize, m_fSize, m_fSize);
	dBodySetMass(m_body, &m_mass);

	dBodySetPosition(m_body, 0.0f, 0.0f, 0.0f);

	dBodySetData(m_body, this);
}

CBonus::~CBonus(void)
{
	dBodyDestroy(m_body);
	dGeomDestroy(m_geom);
}

int CBonus::Draw()
{
	glPushMatrix();

	SetupMatrices();

	GLfloat GreenSurface[] = { 1.0f, 0.0f, 0.0f, 1.0f};
	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, GreenSurface);
	glBegin(GL_QUADS);
	/* przednia sciana */
	glNormal3d( 0.0, 0.0, -1.0);
//	glColor3d(0.0, 1.0,0.0);
	glTexCoord2d(0.0f, 0.0f);	glVertex3d( -0.45, -0.45, -0.45);
	glTexCoord2d(0.0f, 1.0f);	glVertex3d( -0.45, 0.45, -0.45);
	glTexCoord2d(1.0f, 1.0f);	glVertex3d( 0.45, 0.45, -0.45);
	glTexCoord2d(1.0f, 0.0f);	glVertex3d( 0.45, -0.45, -0.45);

	/* tylna sciana */
	glNormal3d( 0.0, 0.0, 1.0);
	glTexCoord2d(0.0f, 0.0f);	glVertex3d( -0.45, -0.45, 0.45);
	glTexCoord2d(0.0f, 1.0f);	glVertex3d( -0.45, 0.45, 0.45);
	glTexCoord2d(1.0f, 1.0f);	glVertex3d( 0.45, 0.45, 0.45);
	glTexCoord2d(1.0f, 0.0f);	glVertex3d( 0.45, -0.45, 0.45);

	/* gorna sciana */
	glNormal3d( 0.0, 1.0, 0.0);
	glTexCoord2d(0.0f, 0.0f);	glVertex3d( -0.45, 0.45, -0.45);
	glTexCoord2d(0.0f, 1.0f);	glVertex3d( -0.45, 0.45, 0.45);
	glTexCoord2d(1.0f, 1.0f);	glVertex3d( 0.45, 0.45, 0.45);
	glTexCoord2d(1.0f, 0.0f);	glVertex3d( 0.45, 0.45, -0.45);

	/* dolna sciana */
	glNormal3d( 0.0, -1.0, 0.0);
	glTexCoord2d(0.0f, 0.0f);	glVertex3d( -0.45, -0.45, -0.45);
	glTexCoord2d(0.0f, 1.0f);	glVertex3d( -0.45, -0.45, 0.45);
	glTexCoord2d(1.0f, 1.0f);	glVertex3d( 0.45, -0.45, 0.45);
	glTexCoord2d(1.0f, 0.0f);	glVertex3d( 0.45, -0.45, -0.45);
	
	/* lewa boczna sciana */
	glNormal3d( -0.0, 0.0, 0.0);
	glTexCoord2d(0.0f, 0.0f);	glVertex3d( -0.45, -0.45, -0.45);
	glTexCoord2d(0.0f, 1.0f);	glVertex3d( -0.45, 0.45, -0.45);
	glTexCoord2d(1.0f, 1.0f);	glVertex3d( -0.45, 0.45, 0.45);
	glTexCoord2d(1.0f, 0.0f);	glVertex3d( -0.45, -0.45, 0.45);

	/* prawa boczna sciana */
	glNormal3d( 0.0, 0.0, 0.0);
	glTexCoord2d(0.0f, 0.0f);	glVertex3d( 0.45, -0.45, -0.45);
	glTexCoord2d(0.0f, 1.0f);	glVertex3d( 0.45, -0.45, 0.45);
	glTexCoord2d(1.0f, 1.0f);	glVertex3d( 0.45, 0.45, 0.45);
	glTexCoord2d(1.0f, 0.0f);	glVertex3d( 0.45, 0.45, -0.45);

	glEnd();

	glPopMatrix();

	return 0;
}

int CBonus::Update()
{
	//move the bonus towards player, and down in y direction
	dBodySetLinearVel(m_body, 0.0f, -m_fSpeed*0.1, m_iSignZ*m_fSpeed);
	return 0;
}

void CBonus::SetBatDest(CBat* pBat)
{
	m_pBatDest = pBat;

	//set direction sign
	const double* dBatPos = m_pBatDest->GetPosition();
	const double* dPos = dBodyGetPosition(m_body);

	//we'll need direction from bonus to bat in z axis
	float fVecZ = (float)(dBatPos[2] - dPos[2]);
	if(fVecZ == 0) fVecZ = 0.001f;	//to avoid division by 0

	m_iSignZ = (int)(fVecZ/abs(fVecZ));	//sign of direction
}

int CBonus::SetPosition(double dX, double dY, double dZ)
{
	dBodySetPosition(m_body, dX, dY, dZ);
	return 0;
}